package com.maben.queue.queue;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * 消费者类
 */
public class QueueConsumer implements Runnable {

    @Override
    public void run() {
        BlockingQueue<Object> queue = MessageQueue.getInstance();
        while (true) {
            try {
                // 从队列中取出元素，如果队列为空则阻塞
                Object value = queue.take();
                System.out.println(
                        Thread.currentThread() +
                                ",queue size:" + queue.size() +
                                ",value:" + value
                );
                // 模拟消费耗时
                TimeUnit.SECONDS.sleep(10);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}